<!-- templates/upload.html -->
{% extends "base.html" %}

{% block title %}上传作品 - 校园摄影投票系统{% endblock %}

{% block content %}
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h4>上传摄影作品</h4>
                </div>
                <div class="card-body">
                    <form method="POST" enctype="multipart/form-data" action="{{ url_for('main.upload') }}">
                        {{ form.hidden_tag() }}
                        
                        <div class="mb-3">
                            {{ form.title.label(class="form-label") }}
                            {{ form.title(class="form-control") }}
                            {% for error in form.title.errors %}
                                <div class="invalid-feedback d-block">{{ error }}</div>
                            {% endfor %}
                        </div>
                        
                        <div class="mb-3">
                            {{ form.description.label(class="form-label") }}
                            {{ form.description(class="form-control", rows=3) }}
                            {% for error in form.description.errors %}
                                <div class="invalid-feedback d-block">{{ error }}</div>
                            {% endfor %}
                        </div>
                        
                        <div class="mb-3">
                            {{ form.category.label(class="form-label") }}
                            {{ form.category(class="form-select") }}
                        </div>
                        
                        <div class="mb-3">
                            {{ form.image.label(class="form-label") }}
                            {{ form.image(class="form-control") }}
                            <div class="form-text">支持格式: JPG, PNG, GIF (最大3MB)</div>
                            {% for error in form.image.errors %}
                                <div class="invalid-feedback d-block">{{ error }}</div>
                            {% endfor %}
                        </div>
                        
                        <!-- 图片预览区域 -->
                        <div class="mb-3">
                            <label class="form-label">图片预览</label>
                            <div id="image-preview" class="border rounded p-2 text-center">
                                <p class="text-muted">选择图片后将在此处预览</p>
                            </div>
                        </div>
                        
                        <button type="submit" class="btn btn-primary">提交作品</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block scripts %}
<script>
// 图片预览功能
document.addEventListener('DOMContentLoaded', function() {
    const imageInput = document.querySelector('input[type="file"]');
    const imagePreview = document.getElementById('image-preview');
    
    imageInput.addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                imagePreview.innerHTML = `
                    <img src="${e.target.result}" class="img-fluid" alt="预览图片" style="max-height: 300px;">
                `;
            };
            reader.readAsDataURL(file);
        }
    });
});
</script>
{% endblock %}